home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 3 / Cream of the Crop 3.iso / c_lang / strpp31.zip / STR.H < prev    next >
C/C++ Source or Header  |  1994-04-18  |  15KB  |  431 lines

  1. /* -------------------------------------------------------------------- */
  2. /* String++ Version 3.10                                       04/13/94 */
  3. /*                                                                      */
  4. /* Enhanced string class for Turbo C++/Borland C++.                     */
  5. /* Copyright 1991-1994 by Carl W. Moreland                              */
  6. /*                                                                      */
  7. /* str.h                                                                */
  8. /* -------------------------------------------------------------------- */
  9.  
  10. #ifndef _STR_H
  11. #define _STR_H
  12.  
  13. #ifndef __CTYPE_H
  14. #include <ctype.h>
  15. #endif
  16. #ifndef __STRING_H
  17. #include <string.h>
  18. #endif
  19. #ifndef __IOSTREAM_H
  20. #include <iostream.h>
  21. #endif
  22.  
  23. class StrPP;
  24. class RegExp;
  25.  
  26. typedef StrPP String;            // Not compatible with BC++ 3.x
  27. //typedef StrPP string;            // Not compatible with BC++ 4.x
  28.  
  29. class StrPP
  30. {
  31. protected:
  32.   int   strLen;                // length of the string
  33.   char* strPtr;                // pointer to the string contents
  34.   int   bufferLen;            // length of the buffer
  35.  
  36. public:
  37.   StrPP();                // default constructor;
  38.   StrPP(const char c,            // initialize with a character,
  39.         const int n = 1);        //   optional # of characters
  40.   StrPP(const char* p);            // initialize with a char *
  41.   StrPP(const char* p,            // initialize with a char *,
  42.         const int pos,            //   optional starting char
  43.         const int len = 32767);        //   optional # of chars
  44.   StrPP(const StrPP& s);        // initialize with another string
  45.   StrPP(const StrPP& s,            // initialize with another string,
  46.         const int pos,            //   optional starting char
  47.         const int len = 32767);        //   optional # of chars
  48.   StrPP(const int n);            // initialize with an integer
  49.   StrPP(const unsigned int n);        // initialize with an unsigned integer
  50.   StrPP(const long n);            // initialize with a long int
  51.   StrPP(const unsigned long n);        // initialize with a unsigned long int
  52.   StrPP(const float n,            // initialize with a float
  53.         const char* format = "");    //   and a format specifier
  54.   StrPP(const double n,            // initialize with a double
  55.         const char* format = "");    //   and a format specifier
  56.  
  57.  ~StrPP(void);
  58.  
  59. protected:
  60.   static int strMinLength;        // minimum memory allocated
  61.   static int strIncLength;        // incremental memory allocated
  62.   static int (*strCompare)(const char*, const char*);
  63.   static int (*strToUpper)(int c);
  64.   static int (*strToLower)(int c);
  65.   static StrPP* findIn;            // these are for the Find... functions
  66.   static StrPP& findStr;
  67.   static int    findPos;
  68.   static StrPP  fpFormat;        // format string for floats
  69.  
  70.   virtual void Init() {
  71.     strPtr = 0;
  72.     strLen = 0;
  73.     bufferLen = 0;
  74.   }
  75.   virtual void SetStr(const char c, const int n = 1);
  76.   virtual void SetStr(const char* p);
  77.   virtual void SetStr(const char* p,   const int pos, const int len = 32767);
  78.   virtual void SetStr(const StrPP& s);
  79.   virtual void SetStr(const StrPP& s, const int pos, const int len = 32767);
  80.   virtual void AddStr(const char c);
  81.   virtual void AddStr(const char* p);
  82.   virtual void AddStr(const StrPP& s);
  83.   virtual int  GetSize(int n);
  84.   virtual void ltos(const long n);
  85.   virtual void ultos(const unsigned long n);
  86.   virtual void dtos(const double n, const char* format);
  87.  
  88. public:
  89.   enum StrModes { LEFT   = 0,
  90.                   CENTER = 1,
  91.                   RIGHT  = 2,
  92.                   NOCLIP = 0,
  93.                   NOTRIM = 0,
  94.                   CLIP   = 1,
  95.                   TRIM   = 2,
  96.                   WHITESPACE = 0 };
  97.  
  98.   static void SetMinLength(int len = 16) { strMinLength = len; }
  99.   static void SetIncLength(int len = 8)  { strIncLength = len; }
  100.   static void SetCompare(int (*fp)(const char*, const char*) = strcmp) {
  101.     strCompare = fp;
  102.   }
  103.   static void SetToUpper(int (*fp)(int c) = ::toupper) {
  104.     strToUpper = fp;
  105.   }
  106.   static void SetToLower(int (*fp)(int c) = ::tolower) {
  107.     strToLower = fp;
  108.   }
  109.   static void SetCaseSensitivity(int cs = 1);
  110.   static void SetFloatFormat(const char*);
  111.   int  SetSize(int len);        // set/reset bufferLen
  112.   void Minimize(void);            // minimize bufferLen
  113.  
  114.   virtual operator const char() const    { return strPtr[0]; }
  115.   virtual operator const char*() const   { return strPtr; }
  116.   virtual const char  operator *() const { return strPtr[0]; }
  117.   virtual const char* operator()() const { return strPtr; }
  118.   virtual const char* operator()(int pos) const { return strPtr+pos; }
  119.   virtual StrPP       operator()(int pos, int len) const;
  120.   virtual char* ptr(void) const          { return strPtr; }
  121.   virtual char* ptr(int pos) const       { return strPtr+pos; }
  122.  
  123.   virtual int Length(void) const { return strLen; }
  124.   virtual int Len(void)    const { return strLen; }
  125.   virtual StrPP& toUpper(void);            // convert to uppercase
  126.   virtual StrPP& toLower(void);            // convert to lowercase
  127.   virtual int&           Value(int& n) const;        // int value of str
  128.   virtual unsigned int&  Value(unsigned int& n) const;    // unsigned value of str
  129.   virtual long&          Value(long& n) const;        // long int value of str
  130.   virtual unsigned long& Value(unsigned long& n) const;    // unsigned long value
  131.   virtual float&         Value(float& n) const;        // float value of str
  132.   virtual double&        Value(double& n) const;    // double value of str
  133.  
  134.   StrPP&  Left(int len);            // left   len chars
  135.   StrPP&  Right(int len);            // right  len chars
  136.   StrPP&  Mid(int pos, int len);        // middle len chars from pos
  137.  
  138.   StrPP&  Justify(char type, int len,        // justify string
  139.           char mode = CLIP|TRIM);
  140.   StrPP&  Trim(int mode = CENTER,        // delete whitespace
  141.                char ch = WHITESPACE);
  142.  
  143.   StrPP&  Insert(int pos, const StrPP& s);    // insert substring
  144.   StrPP&  Delete(int pos, int len = 1);        // delete substring
  145.  
  146.   StrPP&  Replace(int pos, int len,        // substitute pos+len -> to
  147.                   const StrPP& to);
  148.   int     Replace(const StrPP& from,        // substitute from -> to
  149.                   const StrPP& to,
  150.               int count = 32767);
  151.   int     Replace(const RegExp& from,        // substitute from -> to
  152.                   const StrPP& to,
  153.                   int count = 32767);
  154.   char*   Copy(char*&) const;            // copy string to char*
  155.  
  156.   int     Index(const StrPP& t) const;        // position of t in string
  157.   int     Index(const RegExp& t) const;        // position of t in string
  158.   StrPP   SubStr(int pos,             // substring at position pos
  159.                  int len = 32767) const;
  160.   int     Split(StrPP*& a,            // split into an array a on
  161.                 const StrPP& fs) const;        //   field separator fs
  162.   int     Split(StrPP*& a,            // split into an array a on
  163.                 const RegExp& fs) const;    //   field separator fs
  164.   int     Sub(const StrPP& from,        // substitute from -> to
  165.               const StrPP& to,
  166.           int count = 32767);
  167.   int     Sub(const RegExp& from,        // substitute from -> to
  168.               const StrPP& to,
  169.               int count = 32767);
  170.  
  171.   int     FindFirst(const StrPP& s) const;    // first occurance of s
  172.   int     FindNext (void) const;        // next occurance of s
  173.   int     FindPrev (void) const;        // previous occurance of s
  174.   int     FindLast (const StrPP& s) const;    // last occurance of s
  175.  
  176.   virtual StrPP& operator=(const char c);    // str1 = char
  177.   virtual StrPP& operator=(const char* p);    // str1 = char*
  178.   virtual StrPP& operator=(const StrPP& s);    // str1 = string
  179.   virtual StrPP& operator=(const int n);    // str1 = int
  180.   virtual StrPP& operator=(const unsigned int n);  // str1 = uint
  181.   virtual StrPP& operator=(const long n);    // str1 = long
  182.   virtual StrPP& operator=(const unsigned long n); // str1 = ulong
  183.   virtual StrPP& operator=(const float n);    // str1 = float
  184.   virtual StrPP& operator=(const double n);    // str1 = double
  185.   StrPP&  operator+=(const char c);        // str1 += char
  186.   StrPP&  operator+=(const char* p);        // str1 += char*
  187.   StrPP&  operator+=(const StrPP& s);        // str1 += str
  188.   StrPP&  operator*=(const int n);        // str1 *= n
  189.   char&   operator[](const int i) const;    // ch = str[i] or str[i] = ch
  190.  
  191.   friend StrPP operator+(const StrPP& s1, const StrPP& s2);
  192.   friend StrPP operator+(const StrPP& s,  const char* p);
  193.   friend StrPP operator+(const char* p,   const StrPP& s);
  194.   friend StrPP operator*(const StrPP& s,  const int n);
  195.   friend StrPP operator*(const int n,     const StrPP& s);
  196.  
  197.   virtual StrPP& operator<<(const char c);        // s << char
  198.   virtual StrPP& operator<<(const char* p);        // s << char*
  199.   virtual StrPP& operator<<(const StrPP& s);        // s << string
  200.   virtual StrPP& operator<<(const int n);        // s << int
  201.   virtual StrPP& operator<<(const unsigned int n);    // s << uint
  202.   virtual StrPP& operator<<(const long n);        // s << long
  203.   virtual StrPP& operator<<(const unsigned long n);    // s << ulong
  204.   virtual StrPP& operator<<(const float n);        // s << float
  205.   virtual StrPP& operator<<(const double n);        // s << double
  206.  
  207.   friend int operator==(const StrPP& s1, const StrPP& s2);
  208.   friend int operator!=(const StrPP& s1, const StrPP& s2);
  209.   friend int operator< (const StrPP& s1, const StrPP& s2);
  210.   friend int operator> (const StrPP& s1, const StrPP& s2);
  211.   friend int operator<=(const StrPP& s1, const StrPP& s2);
  212.   friend int operator>=(const StrPP& s1, const StrPP& s2);
  213.   friend int operator==(const StrPP& s,  const char* p);
  214.   friend int operator!=(const StrPP& s,  const char* p);
  215.   friend int operator< (const StrPP& s,  const char* p);
  216.   friend int operator> (const StrPP& s,  const char* p);
  217.   friend int operator<=(const StrPP& s,  const char* p);
  218.   friend int operator>=(const StrPP& s,  const char* p);
  219.   friend int operator==(const char* p,   const StrPP& s);
  220.   friend int operator!=(const char* p,   const StrPP& s);
  221.   friend int operator< (const char* p,   const StrPP& s);
  222.   friend int operator> (const char* p,   const StrPP& s);
  223.   friend int operator<=(const char* p,   const StrPP& s);
  224.   friend int operator>=(const char* p,   const StrPP& s);
  225. };
  226.  
  227. ostream& operator<<(ostream&, const StrPP&);
  228. istream& operator>>(istream&, StrPP&);
  229.  
  230. /* ----- Awk-style functions ------------------------------------------ */
  231.  
  232. inline int length(const char* p) {
  233.   return strlen(p);
  234. }
  235. inline int length(const StrPP& s) {
  236.   return s.Len();
  237. }
  238. int    index(const char* s, const char* t);
  239. StrPP substr(const StrPP& s, int pos, int len = 32767);
  240. int    split(const StrPP& s, StrPP*& a, const StrPP& fs);
  241. int     gsub(const StrPP& from, const StrPP& to, StrPP& str, int count = 32767);
  242. inline int sub(const StrPP& from, const StrPP& to, StrPP& str) {
  243.   return gsub(from, to, str, 1);
  244. }
  245.  
  246. // Regular Expression versions - defined in regexp.cpp
  247.  
  248. extern int index(const StrPP& s, const RegExp& t);
  249. extern int split(const StrPP& s, StrPP*& a, const RegExp& fs);
  250. extern int  gsub(const RegExp& from, const StrPP& to, StrPP& str, int count);
  251. extern int   sub(const RegExp& from, const StrPP& to, StrPP& str);
  252.  
  253. /* ----- Other C-style functions -------------------------------------- */
  254.  
  255. StrPP toupper(const StrPP& s);
  256. StrPP tolower(const StrPP& s);
  257. StrPP    left(const char* p, int len);
  258. StrPP   right(const char* p, int len);
  259. StrPP     mid(const char* p, int pos, int len);
  260. StrPP justify(const char* p, char type, int len,
  261.               char mode=StrPP::CLIP|StrPP::TRIM);
  262. StrPP trim(const char* p, int mode = StrPP::CENTER);
  263.  
  264. /* ----- Inline functions --------------------------------------------- */
  265.  
  266. inline StrPP& StrPP::operator=(const int n) {
  267.   operator=((long)n);
  268.   return *this;
  269. }
  270.  
  271. inline StrPP& StrPP::operator=(const unsigned int n) {
  272.   operator=((unsigned long)n);
  273.   return *this;
  274. }
  275.  
  276. inline StrPP& StrPP::operator=(const float n) {
  277.   operator=((double)n);
  278.   return *this;
  279. }
  280.  
  281. inline StrPP& StrPP::operator<<(const int n) {
  282.   operator<<((long)n);
  283.   return *this;
  284. }
  285.  
  286. inline StrPP& StrPP::operator<<(const unsigned int n) {
  287.   operator<<((unsigned long)n);
  288.   return *this;
  289. }
  290.  
  291. inline StrPP& StrPP::operator<<(const float n) {
  292.   operator<<((double)n);
  293.   return *this;
  294. }
  295.  
  296. inline int StrPP::Replace(const StrPP& from, const StrPP& to, int count) {
  297.   return gsub(from, to, *this, count);
  298. }
  299.  
  300. inline int StrPP::Replace(const RegExp& from, const StrPP& to, int count) {
  301.   return gsub(from, to, *this, count);
  302. }
  303.  
  304. inline int operator==(const StrPP& s1, const StrPP& s2) {
  305.   return StrPP::strCompare(s1, s2) == 0;
  306. }
  307.  
  308. inline int operator!=(const StrPP& s1, const StrPP& s2) {
  309.   return StrPP::strCompare(s1, s2) != 0;
  310. }
  311.  
  312. inline int operator<(const StrPP& s1, const StrPP& s2) {
  313.   return StrPP::strCompare(s1, s2) < 0;
  314. }
  315.  
  316. inline int operator>(const StrPP& s1, const StrPP& s2) {
  317.   return StrPP::strCompare(s1, s2) > 0;
  318. }
  319.  
  320. inline int operator<=(const StrPP& s1, const StrPP& s2) {
  321.   return StrPP::strCompare(s1, s2) <= 0;
  322. }
  323.  
  324. inline int operator>=(const StrPP& s1, const StrPP& s2) {
  325.   return StrPP::strCompare(s1, s2) >= 0;
  326. }
  327.  
  328. inline int operator==(const StrPP& s, const char* p) {
  329.   return StrPP::strCompare(s, p) == 0;
  330. }
  331.  
  332. inline int operator!=(const StrPP& s, const char* p) {
  333.   return StrPP::strCompare(s, p) != 0;
  334. }
  335.  
  336. inline int operator<(const StrPP& s, const char* p) {
  337.   return StrPP::strCompare(s, p) < 0;
  338. }
  339.  
  340. inline int operator>(const StrPP& s, const char* p) {
  341.   return StrPP::strCompare(s, p) > 0;
  342. }
  343.  
  344. inline int operator<=(const StrPP& s, const char* p) {
  345.   return StrPP::strCompare(s, p) <= 0;
  346. }
  347.  
  348. inline int operator>=(const StrPP& s, const char* p) {
  349.   return StrPP::strCompare(s, p) >= 0;
  350. }
  351.  
  352. inline int operator==(const char* p, const StrPP& s) {
  353.   return StrPP::strCompare(p, s) == 0;
  354. }
  355.  
  356. inline int operator!=(const char* p, const StrPP& s) {
  357.   return StrPP::strCompare(p, s) != 0;
  358. }
  359.  
  360. inline int operator<(const char* p, const StrPP& s) {
  361.   return StrPP::strCompare(p, s) < 0;
  362. }
  363.  
  364. inline int operator>(const char* p, const StrPP& s) {
  365.   return StrPP::strCompare(p, s) > 0;
  366. }
  367.  
  368. inline int operator<=(const char* p, const StrPP& s) {
  369.   return StrPP::strCompare(p, s) <= 0;
  370. }
  371.  
  372. inline int operator>=(const char* p, const StrPP& s) {
  373.   return StrPP::strCompare(p, s) >= 0;
  374. }
  375.  
  376. inline int operator==(const StrPP& s, const char c) {
  377.   return *s == c;
  378. }
  379.  
  380. inline int operator!=(const StrPP& s, const char c) {
  381.   return *s != c;
  382. }
  383.  
  384. inline int operator<(const StrPP& s, const char c) {
  385.   return *s < c;
  386. }
  387.  
  388. inline int operator>(const StrPP& s, const char c) {
  389.   return *s > c;
  390. }
  391.  
  392. inline int operator<=(const StrPP& s, const char c) {
  393.   return *s <= c;
  394. }
  395.  
  396. inline int operator>=(const StrPP& s, const char c) {
  397.   return *s >= c;
  398. }
  399.  
  400. inline int StrPP::Index(const StrPP& t) const {
  401.   return index(*this, t);
  402. }
  403.  
  404. inline int StrPP::Index(const RegExp& t) const {
  405.   return index(*this, t);
  406. }
  407.  
  408. inline StrPP StrPP::SubStr(int p, int n) const {
  409.   return substr(*this, p, n);
  410. }
  411.  
  412. inline int StrPP::Split(StrPP*& a, const StrPP& fs) const {
  413.   return split(*this, a, fs);
  414. }
  415.  
  416. inline int StrPP::Split(StrPP*& a, const RegExp& fs) const {
  417.   return split(*this, a, fs);
  418. }
  419.  
  420. inline int StrPP::Sub(const StrPP& from, const StrPP& to, int count) {
  421.   return gsub(from, to, *this, count);
  422. }
  423.  
  424. inline int StrPP::Sub(const RegExp& from, const StrPP& to, int count) {
  425.   return gsub(from, to, *this, count);
  426. }
  427.  
  428. extern const StrPP STR_NULL;        // a global NULL string
  429.  
  430. #endif
  431.